package org.dsun.mvc.filter;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GlobalDistributeFilter implements Filter {
	
	private FilterConfig config;

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse resp=(HttpServletResponse)response;
		try {
			GlobalDistributeFilterUtil.DistributeByConfig(req, resp, chain, config);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		try {
//			Class c=Class.forName("org.dsun.mvc.action.TestAction");
//			try {
//				Method m=c.getMethod("test");
//				try {
//					Object o=c.newInstance();
//					Seter.invokeSeter(o, "request", req);
//					Seter.invokeSeter(o, "response", resp);
//					try {
//						m.invoke(o);
//						req.setAttribute("URL", Geter.invokeGeter(o, "url"));
//						req.setAttribute("name", Geter.invokeGeter(o, "name"));
//					} catch (IllegalArgumentException e) {
//						e.printStackTrace();
//					} catch (InvocationTargetException e) {
//						e.printStackTrace();
//					}
//				} catch (InstantiationException e1) {
//					e1.printStackTrace();
//				} catch (IllegalAccessException e1) {
//					e1.printStackTrace();
//				}
//			} catch (SecurityException e) {
//				e.printStackTrace();
//			} catch (NoSuchMethodException e) {
//				e.printStackTrace();
//			}
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		}
//		
//		RequestDispatcher rd=request.getRequestDispatcher("/pages/text.jsp");
//		rd.forward(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config=config;
	}

}
